*Performs R^2 and utility gain analysis for forecasts with PTV restrictions

set more 1

do ptvprepare

*Generate mean forecasts
capture drop pred spred
egen pred=rowmean(ptvfxx*)
egen spred=rowmean(ptvsfxx*)

do evaluation

capture drop what
capture gen what="PTV mean" in 1
local s="ResPTVmean$c$h$sep$freq"
local ds="DataPTVmean$c$h$sep$freq"

export excel what oos* cduoptsmodelmean* cduoptbuyhold* using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)
export excel yy* pred lnexret modelmean using results.xlsx if yyy>=$firstpred, sheetmod sheet("`s'") cell(A10) first(var)


*with weighted LS
capture drop pred spred

egen pred=rowmean(wptvfxx*)
egen spred=rowmean(wptvsfxx*)

do evaluation

capture drop what
gen what="wPTV mean " in 1
local s="wResPTVmean$c$h$sep$freq"


export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)



*Generate median forecasts
capture drop pred spred
egen pred=rowmedian(ptvfxx*)
egen spred=rowmedian(ptvsfxx*)

do evaluation

capture drop what
gen what="PTV median" in 1
local s="ResPTVmedian$c$h$sep$freq"
local ds="DataPTVmedian$c$h$sep$freq"



export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)
export excel yy* pred lnexret modelmean using results.xlsx if yyy>=$firstpred, sheetmod sheet("`s'") cell(A10) first(var)



*with weighted LS
capture drop pred spred

egen pred=rowmedian(wptvfxx*)
egen spred=rowmedian(wptvsfxx*)

do evaluation


capture drop what
gen what="wPTVmedian " in 1
local s="wResPTVmedian$c$h$sep$freq"


export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)




*Generate trimmed mean forecasts
if $freq==4 {
	local n=13
}
if $freq==12 {
	local n=12
}

capture drop pred spred
capture drop xrowsum-xrowmax
egen xrowsum=rowtotal(ptvfxx*)
egen xrowmin=rowmin(ptvfxx*)
egen xrowmax=rowmax(ptvfxx*)
gen pred=(xrowsum-xrowmin-xrowmax)/`n'

capture drop xrowsum-xrowmax
egen xrowsum=rowtotal(ptvsfxx*)
egen xrowmin=rowmin(ptvsfxx*)
egen xrowmax=rowmax(ptvsfxx*)
gen spred=(xrowsum-xrowmin-xrowmax)/`n'



do evaluation

capture drop what
gen what="PTV trimmed" in 1
local s="ResPTVtrim$c$h$sep$freq"
local ds="DataPTVtrim$c$h$sep$freq"


export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)
export excel yy* pred lnexret modelmean using results.xlsx if yyy>=$firstpred, sheetmod sheet("`s'") cell(A10) first(var)



*with weighted LS

capture drop pred spred
capture drop xrowsum-xrowmax
egen xrowsum=rowtotal(wptvfxx*)
egen xrowmin=rowmin(wptvfxx*)
egen xrowmax=rowmax(wptvfxx*)
gen pred=(xrowsum-xrowmin-xrowmax)/`n'

capture drop xrowsum-xrowmax
egen xrowsum=rowtotal(wptvsfxx*)
egen xrowmin=rowmin(wptvsfxx*)
egen xrowmax=rowmax(wptvsfxx*)
gen spred=(xrowsum-xrowmin-xrowmax)/`n'



do evaluation


capture drop what
gen what="wPTV trimmed" in 1
local s="wResPTVtrim$c$h$sep$freq"


export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)

